package com.meichis.yndms.common;

import com.meichis.mcsappframework.entity.Attachment;
import com.meichis.mcsappframework.executor.MCSExecutorService;
import com.meichis.mcsappframework.executor.MCSRunnable;
import com.meichis.yndms.model.Picture;
import com.meichis.yndms.room.AppDatabase;
import com.meichis.yndms.ui.view.IPictureView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by pei on 2016/9/2.
 */
public class PictureUtils {
    public static int deletePicture(Picture picture) {
        if (picture == null)
            return 0;
        File file = new File(picture.getFilePath());
        if (file.exists())
            file.delete();
        return AppDatabase.getInstance().pictureDao().delete(picture);
    }

    public static int deletePicture(List<Picture> pictures) {
        if (pictures == null || pictures.size() == 0)
            return 0;
        for (Picture picture : pictures) {
            File file = new File(picture.getFilePath());
            if (file.exists())
                file.delete();
        }
        return AppDatabase.getInstance().pictureDao().delete(pictures);
    }

    public static int deletePicture(String PhotosGUID) {
        if (PhotosGUID == null || PhotosGUID.trim().equals(""))
            return 0;
        Picture picture;
        String[] guids = PhotosGUID.split("\\|");
        for (String s : guids) {
            picture = AppDatabase.getInstance().pictureDao().find(s);
            if (picture != null && picture.getFilePath().length() > 0) {
                File file = new File(picture.getFilePath());
                if (file.exists())
                    file.delete();
            }
        }
        return AppDatabase.getInstance().pictureDao().delete(guids);
    }

    public static Picture Attachment2Picture(Attachment attachment) {
        Picture picture = AppDatabase.getInstance().pictureDao().find(attachment.getGUID());
        if (picture == null) {
            picture = new Picture();
            picture.setGuid(attachment.getGUID());
        }
        return picture;
    }

    public static List<Picture> Attachment2Picture(List<Attachment> attachments) {
        if (attachments == null || attachments.size() == 0)
            return new ArrayList<>();
        String[] guids = new String[attachments.size()];
        for (int i = 0; i < guids.length; i++) {
            guids[i] = attachments.get(i).getGUID();
        }
        return (ArrayList<Picture>) AppDatabase.getInstance().pictureDao().find(guids);
    }

    public static Attachment Picture2Attachment(Picture picture) {
        if (picture == null)
            return new Attachment();
        Attachment attachment = new Attachment();
        attachment.setGUID(picture.getGuid());
        attachment.setAttName(picture.getFillName().substring(0, picture.getFillName().lastIndexOf(".")));
        attachment.setExtName(picture.getFillName().substring(picture.getFillName().lastIndexOf(".") + 1));
        return attachment;
    }

    public static List<Attachment> Picture2Attachment(List<Picture> pictures) {
        if (pictures == null || pictures.size() == 0)
            return new ArrayList<>();
        List<Attachment> attachments = new ArrayList<>();
        for (Picture picture : pictures) {
            attachments.add(Picture2Attachment(picture));
        }
        return attachments;
    }

    public static String Picture2Guid(List<Picture> pictures) {
        if (pictures == null || pictures.size() == 0)
            return "";
        StringBuilder builder = new StringBuilder();
        for (Picture picture : pictures) {
            builder.append("|").append(picture.getGuid());
        }
        return builder.substring(1);
    }

    public static List<Picture> Guid2Picture(String Guid) {
        List<Picture> pictures = new ArrayList<>();
        if (Guid == null || Guid.length() == 0)
            return pictures;
        Picture picture;
        for (String s : Guid.split("\\|")) {
            picture = AppDatabase.getInstance().pictureDao().find(s);
            if (picture == null || !new File(picture.getFilePath()).exists()) {
                picture = new Picture();
                picture.setGuid(s);
            }
            pictures.add(picture);
        }
        return pictures;
    }

    public static void Guid2Picture(final String Guid, final IPictureView<ArrayList<Picture>> baseView) {
        MCSExecutorService.getInstance().execute(new MCSRunnable() {
            ArrayList<Picture> pictures = new ArrayList<>();

            @Override
            protected void doBackground() {
                if (Guid == null || Guid.length() == 0) {
                    return;
                }
                Picture picture;
                for (String s : Guid.split("\\|")) {
                    picture = AppDatabase.getInstance().pictureDao().find(s);
                    if (picture == null || !new File(picture.getFilePath()).exists()) {
                        picture = new Picture();
                        picture.setGuid(s);
                    }
                    pictures.add(picture);
                }
            }

            @Override
            protected void completeOnMain() {
                baseView.onReceive(pictures);
            }
        });
    }

    public static void insertPicture(final Picture picture) {
        insertPicture(picture, null);
    }

    public static void insertPicture(final Picture picture, final IPictureView baseView) {
        if (picture == null) {
            if (baseView != null) {
                baseView.onReceive(null);
            }
            return;
        }
        if (baseView != null) {
            MCSExecutorService.getInstance().execute(new MCSRunnable() {
                @Override
                protected void doBackground() {
                    AppDatabase.getInstance().pictureDao().insert(picture);
                }

                @Override
                protected void completeOnMain() {
                    baseView.onReceive(null);
                }
            });
        } else {
            MCSExecutorService.getInstance().execute(new Runnable() {
                @Override
                public void run() {
                    AppDatabase.getInstance().pictureDao().insert(picture);
                }
            });
        }
    }


    public static void deletePictureNew(final String PhotosGUID) {
        deletePicture(PhotosGUID, null);
    }

    public static void deletePicture(final String PhotosGUID, final IPictureView baseView) {
        if (PhotosGUID == null || PhotosGUID.trim().equals("")) {
            if (baseView != null) {
                baseView.onReceive(null);
            }
            return;
        }

        final String[] guids = PhotosGUID.split("\\|");

        if (baseView != null) {
            MCSExecutorService.getInstance().execute(new MCSRunnable() {
                @Override
                protected void doBackground() {
                    Picture picture;
                    for (String s : guids) {
                        picture = AppDatabase.getInstance().pictureDao().find(s);
                        if (picture != null && picture.getFilePath().length() > 0) {
                            File file = new File(picture.getFilePath());
                            if (file.exists())
                                file.delete();
                        }
                    }
                    AppDatabase.getInstance().pictureDao().delete(guids);
                }

                @Override
                protected void completeOnMain() {
                    baseView.onReceive(null);
                }
            });
        } else {
            MCSExecutorService.getInstance().execute(new Runnable() {
                @Override
                public void run() {
                    AppDatabase.getInstance().pictureDao().delete(guids);
                }
            });
        }
    }

}
